package com.sunmi.usercenter.ui.activity.fragment;

import java.lang.reflect.Field;

import com.sunmi.usercenter.Constant;
import com.sunmi.usercenter.bean.FmInfo;
import com.sunmi.usercenter.controller.FragmentController;
import com.sunmi.usercenter.controller.HttpController;
import com.sunmi.usercenter.model.CaCheModel;
import com.sunmi.usercenter.ui.activity.HomeActivity;
import com.sunmi.usercenter.utils.CharUtils;
import com.sunmi.usercenter.utils.ThreadPoolManager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 项目名称：Sunmi-User-Center
 * 类描述：
 * 创建人：Abtswiath丶lxy
 * 创建时间：2016/6/8 10:37
 * 修改人：longx
 * 修改时间：2016/6/8 10:37
 * 修改备注：
 */
public class SuperFragment extends Fragment {
    /**
     * 上下文
     */
    public Context mContext;
    /**
     * activity
     */
    public HomeActivity mActivity;
    private FragmentController mFragmentController;
    /**
     * 线程池
     */
    public ThreadPoolManager mThreadPoolManager;
    /**
     * 传递数据
     */
    private Object info;

    public HttpController mHttpController;
    
    public CaCheModel mCaCheModel;

    
    public Object getInfo() {
        return info;
    }

    public void setInfo(Object info) {
        this.info = info;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mContext = getActivity();
        mActivity = (HomeActivity) mContext;
        mThreadPoolManager = ThreadPoolManager.getInstance();
        mFragmentController = FragmentController.getInstance(mContext, getFragmentManager());
        mHttpController = HttpController.getInstance(mContext);
        mCaCheModel = CaCheModel.getInstance(mContext);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    /**
     * title居中显示
     * @param textView
     */
    public void layoutTitle(TextView textView){
        if(!Constant.ME_IS_LAUNCHER) {
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            layoutParams.topMargin = 74;
            layoutParams.leftMargin = 10;
            layoutParams.rightMargin = 10;
            textView.setLayoutParams(layoutParams);
        }
    }
    
    /**
     * 打开一个Fragment
     *
     * @param fmInfo
     */
    public void openFM(FmInfo fmInfo) {
    	mFragmentController.openFM(fmInfo);
    }
    
    @SuppressLint("NewApi")
	public void exitMe(){
        Settings.Secure.putInt(mActivity.getContentResolver(), "usercenter_finished", 1);
        if(CharUtils.isAppInstalled(mContext,"com.woyou.channelservice")) {
            android.provider.Settings.Global.putString(getActivity().getContentResolver(), "custom_launcher", "com.woyou.channelservice");
        }else if(CharUtils.isAppInstalled(mContext,"com.woyou.launcher")){
            android.provider.Settings.Global.putString(getActivity().getContentResolver(), "custom_launcher", "com.woyou.launcher");
        }else{
            android.provider.Settings.Global.putString(getActivity().getContentResolver(), "custom_launcher", "com.android.launcher3");
        }
        triggerHomeKey();
        System.exit(0);
    }


    /**
     * 触发Home按钮
     */
    private void triggerHomeKey() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        startActivity(intent);
    }

    /**
     * UI线程执行一个任务
     *
     * @param run
     */
    public void runOnUI(Runnable run) {
        mActivity.runOnUiThread(run);
    }

    public void showToast(String msg){
    	Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }
    /**
     * 子线程运行
     *
     * @param run
     */
    public void executeTask(Runnable run) {
        mThreadPoolManager.executeTask(run);
    }

    public void intentWifi(){
    	Intent intent = new Intent();
    	intent.setAction("android.net.wifi.PICK_WIFI_NETWORK");
    	startActivity(intent);
    }

    @Override
    public void onDetach() {
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        super.onDetach();
    }

    protected  void onBack(){
        Constant.CLASS_FRAGMENT = "";
        mActivity.onBackPressed();
    }
}
